/*!
 * \file test_wts_trade_def.cpp
 * \project WonderTrader
 *
 * \author AI Assistant
 * \date 2024/12/19
 * 
 * \brief WTS交易数据定义测试文件
 * 
 * \details 测试WTS交易相关的核心数据类：
 *          - WTSEntrust：委托数据结构测试
 *          - WTSOrderInfo：委托订单信息测试
 *          - WTSTradeInfo：成交信息测试
 *          - WTSPositionItem：持仓信息测试
 *          - WTSAccountInfo：账户信息测试
 */

#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <memory>
#include <string>
#include <cstring>

#include "../../src/Includes/WTSTradeDef.hpp"
#include "../../src/Includes/WTSContractInfo.hpp"

using namespace wtp;

/*!
 * \class WTSTradeDefTest
 * \brief WTS交易数据定义测试类
 * 
 * \details 测试WTS交易数据定义相关的所有核心类和功能
 */
class WTSTradeDefTest : public ::testing::Test {
protected:
    void SetUp() override {
        // 创建测试用的合约信息
        contract_info = WTSContractInfo::create("au2412", "黄金2412", "SHFE", "au");
        ASSERT_NE(contract_info, nullptr);
    }

    void TearDown() override {
        if (contract_info) {
            contract_info->release();
            contract_info = nullptr;
        }
    }

    WTSContractInfo* contract_info = nullptr;
};

/*! 
 * \brief 测试WTSEntrust委托数据结构
 * 
 * \details 验证委托数据的创建、设置和获取：
 *          - 委托基础信息设置
 *          - 价格和数量管理
 *          - 交易属性设置
 *          - 业务类型管理
 */
TEST_F(WTSTradeDefTest, TestWTSEntrust) {
    // 创建委托对象
    WTSEntrust* entrust = WTSEntrust::create("au2412", 10.0, 380.5, "SHFE", BT_CASH);
    ASSERT_NE(entrust, nullptr);
    
    // 测试基础信息
    EXPECT_STREQ(entrust->getCode(), "au2412");
    EXPECT_STREQ(entrust->getExchg(), "SHFE");
    EXPECT_DOUBLE_EQ(entrust->getVolume(), 10.0);
    EXPECT_DOUBLE_EQ(entrust->getPrice(), 380.5);
    
    // 测试业务类型
    entrust->setBusinessType(BT_CASH);
    EXPECT_EQ(entrust->getBusinessType(), BT_CASH);
    
    // 测试交易属性设置
    entrust->setDirection(WDT_LONG);
    entrust->setPriceType(WPT_LIMITPRICE);
    entrust->setOrderFlag(WOF_NOR);
    entrust->setOffsetType(WOT_OPEN);
    
    EXPECT_EQ(entrust->getDirection(), WDT_LONG);
    EXPECT_EQ(entrust->getPriceType(), WPT_LIMITPRICE);
    EXPECT_EQ(entrust->getOrderFlag(), WOF_NOR);
    EXPECT_EQ(entrust->getOffsetType(), WOT_OPEN);
    
    // 测试委托编号和用户标签
    entrust->setEntrustID("E001");
    entrust->setUserTag("TestTag");
    EXPECT_STREQ(entrust->getEntrustID(), "E001");
    EXPECT_STREQ(entrust->getUserTag(), "TestTag");
    
    // 测试净持仓模式
    entrust->setNetDirection(true);
    EXPECT_TRUE(entrust->isNet());
    EXPECT_TRUE(entrust->isBuy());
    
    // 测试合约信息关联
    entrust->setContractInfo(contract_info);
    EXPECT_EQ(entrust->getContractInfo(), contract_info);
    
    entrust->release();
}

/*! 
 * \brief 测试WTSOrderInfo订单信息
 * 
 * \details 验证订单信息的管理：
 *          - 订单基础信息
 *          - 订单状态管理
 *          - 成交信息跟踪
 *          - 错误状态处理
 */
TEST_F(WTSTradeDefTest, TestWTSOrderInfo) {
    // 创建订单信息对象
    WTSOrderInfo* order = WTSOrderInfo::create();
    ASSERT_NE(order, nullptr);
    
    // 设置基础信息
    order->setCode("au2412");
    order->setExchange("SHFE");
    EXPECT_STREQ(order->getCode(), "au2412");
    EXPECT_STREQ(order->getExchg(), "SHFE");
    
    // 设置订单属性
    order->setDirection(WDT_LONG);
    order->setPriceType(WPT_LIMITPRICE);
    order->setOrderFlag(WOF_NOR);
    order->setOffsetType(WOT_OPEN);
    order->setVolume(10.0);
    order->setPrice(380.5);
    
    EXPECT_EQ(order->getDirection(), WDT_LONG);
    EXPECT_EQ(order->getPriceType(), WPT_LIMITPRICE);
    EXPECT_EQ(order->getOrderFlag(), WOF_NOR);
    EXPECT_EQ(order->getOffsetType(), WOT_OPEN);
    EXPECT_DOUBLE_EQ(order->getVolume(), 10.0);
    EXPECT_DOUBLE_EQ(order->getPrice(), 380.5);
    
    // 测试订单状态管理
    order->setOrderState(WOS_NotTraded_Queuing);
    order->setOrderType(WORT_Normal);
    order->setOrderDate(20241219);
    order->setOrderTime(93015000);
    
    EXPECT_EQ(order->getOrderState(), WOS_NotTraded_Queuing);
    EXPECT_EQ(order->getOrderType(), WORT_Normal);
    EXPECT_EQ(order->getOrderDate(), 20241219);
    EXPECT_EQ(order->getOrderTime(), 93015000);
    
    // 测试成交信息
    order->setVolTraded(5.0);
    order->setVolLeft(5.0);
    EXPECT_DOUBLE_EQ(order->getVolTraded(), 5.0);
    EXPECT_DOUBLE_EQ(order->getVolLeft(), 5.0);
    
    // 测试订单状态判断
    EXPECT_TRUE(order->isAlive());
    EXPECT_FALSE(order->isError());
    
    // 测试错误状态
    order->setError(true);
    EXPECT_TRUE(order->isError());
    
    order->release();
}

/*! 
 * \brief 测试WTSTradeInfo成交信息
 * 
 * \details 验证成交信息的管理：
 *          - 成交基础信息
 *          - 成交价格和数量
 *          - 成交时间管理
 *          - 关联订单信息
 */
TEST_F(WTSTradeDefTest, TestWTSTradeInfo) {
    // 创建成交信息对象
    WTSTradeInfo* trade = WTSTradeInfo::create("au2412", "SHFE");
    ASSERT_NE(trade, nullptr);
    
    // 测试基础信息
    EXPECT_STREQ(trade->getCode(), "au2412");
    EXPECT_STREQ(trade->getExchg(), "SHFE");
    
    // 设置成交信息
    trade->setDirection(WDT_LONG);
    trade->setOffsetType(WOT_OPEN);
    trade->setVolume(5.0);
    trade->setPrice(380.6);
    trade->setAmount(1903.0);
    
    EXPECT_EQ(trade->getDirection(), WDT_LONG);
    EXPECT_EQ(trade->getOffsetType(), WOT_OPEN);
    EXPECT_DOUBLE_EQ(trade->getVolume(), 5.0);
    EXPECT_DOUBLE_EQ(trade->getPrice(), 380.6);
    EXPECT_DOUBLE_EQ(trade->getAmount(), 1903.0);
    
    // 设置成交时间
    trade->setTradeDate(20241219);
    trade->setTradeTime(93015500);
    EXPECT_EQ(trade->getTradeDate(), 20241219);
    EXPECT_EQ(trade->getTradeTime(), 93015500);
    
    // 设置关联信息
    trade->setTradeID("T001");
    trade->setRefOrder("O001");
    EXPECT_STREQ(trade->getTradeID(), "T001");
    EXPECT_STREQ(trade->getRefOrder(), "O001");
    
    // 测试成交类型
    trade->setTradeType(WTT_Common);
    trade->setOrderType(WORT_Normal);
    EXPECT_EQ(trade->getTradeType(), WTT_Common);
    EXPECT_EQ(trade->getOrderType(), WORT_Normal);
    
    trade->release();
}

/*! 
 * \brief 测试WTSPositionItem持仓信息
 * 
 * \details 验证持仓信息的管理：
 *          - 持仓基础信息
 *          - 持仓数量管理
 *          - 持仓成本和盈亏
 *          - 保证金计算
 */
TEST_F(WTSTradeDefTest, TestWTSPositionItem) {
    // 创建持仓信息对象
    WTSPositionItem* position = WTSPositionItem::create("au2412", "CNY", "SHFE");
    ASSERT_NE(position, nullptr);
    
    // 测试基础信息
    EXPECT_STREQ(position->getCode(), "au2412");
    EXPECT_STREQ(position->getCurrency(), "CNY");
    EXPECT_STREQ(position->getExchg(), "SHFE");
    
    // 设置持仓方向
    position->setDirection(WDT_LONG);
    EXPECT_EQ(position->getDirection(), WDT_LONG);
    
    // 设置持仓数量
    position->setPrePosition(8.0);
    position->setNewPosition(2.0);
    position->setAvailPrePos(6.0);
    position->setAvailNewPos(2.0);
    
    EXPECT_DOUBLE_EQ(position->getPrePosition(), 8.0);
    EXPECT_DOUBLE_EQ(position->getNewPosition(), 2.0);
    EXPECT_DOUBLE_EQ(position->getAvailPrePos(), 6.0);
    EXPECT_DOUBLE_EQ(position->getAvailNewPos(), 2.0);
    
    // 测试计算属性
    EXPECT_DOUBLE_EQ(position->getTotalPosition(), 10.0);
    EXPECT_DOUBLE_EQ(position->getAvailPosition(), 8.0);
    EXPECT_DOUBLE_EQ(position->getFrozenPosition(), 2.0);
    EXPECT_DOUBLE_EQ(position->getFrozenPrePos(), 2.0);
    EXPECT_DOUBLE_EQ(position->getFrozenNewPos(), 0.0);
    
    // 设置成本和盈亏
    position->setPositionCost(3800.0);
    position->setAvgPrice(380.0);
    position->setDynProfit(300.0);
    position->setMargin(1520.0);
    
    EXPECT_DOUBLE_EQ(position->getPositionCost(), 3800.0);
    EXPECT_DOUBLE_EQ(position->getAvgPrice(), 380.0);
    EXPECT_DOUBLE_EQ(position->getDynProfit(), 300.0);
    EXPECT_DOUBLE_EQ(position->getMargin(), 1520.0);
    
    position->release();
}

/*! 
 * \brief 测试WTSAccountInfo账户信息
 * 
 * \details 验证账户信息的管理：
 *          - 账户基础信息
 *          - 资金管理
 *          - 盈亏统计
 *          - 保证金计算
 */
TEST_F(WTSTradeDefTest, TestWTSAccountInfo) {
    // 创建账户信息对象
    WTSAccountInfo* account = WTSAccountInfo::create();
    ASSERT_NE(account, nullptr);
    
    // 设置账户基础信息
    account->setDescription("测试账户");
    account->setCurrency("CNY");
    EXPECT_STREQ(account->getDescription(), "测试账户");
    EXPECT_STREQ(account->getCurrency(), "CNY");
    
    // 设置资金信息
    account->setBalance(100000.0);
    account->setPreBalance(95000.0);
    account->setAvailable(85000.0);
    account->setMargin(15000.0);
    account->setFrozenMargin(2000.0);
    
    EXPECT_DOUBLE_EQ(account->getBalance(), 100000.0);
    EXPECT_DOUBLE_EQ(account->getPreBalance(), 95000.0);
    EXPECT_DOUBLE_EQ(account->getAvailable(), 85000.0);
    EXPECT_DOUBLE_EQ(account->getMargin(), 15000.0);
    EXPECT_DOUBLE_EQ(account->getFrozenMargin(), 2000.0);
    
    // 设置盈亏信息
    account->setCloseProfit(2500.0);
    account->setDynProfit(2500.0);
    account->setCommission(150.0);
    account->setFrozenCommission(25.0);
    
    EXPECT_DOUBLE_EQ(account->getCloseProfit(), 2500.0);
    EXPECT_DOUBLE_EQ(account->getDynProfit(), 2500.0);
    EXPECT_DOUBLE_EQ(account->getCommission(), 150.0);
    EXPECT_DOUBLE_EQ(account->getFrozenCommission(), 25.0);
    
    // 设置出入金信息
    account->setDeposit(5000.0);
    account->setWithdraw(0.0);
    EXPECT_DOUBLE_EQ(account->getDeposit(), 5000.0);
    EXPECT_DOUBLE_EQ(account->getWithdraw(), 0.0);
    
    account->release();
}

/*! 
 * \brief 测试交易数据的内存管理
 * 
 * \details 验证交易数据对象的内存管理：
 *          - 对象创建和销毁
 *          - 引用计数管理
 *          - 大量对象处理
 *          - 内存泄漏检测
 */
TEST_F(WTSTradeDefTest, TestMemoryManagement) {
    // 测试委托对象内存管理
    {
        WTSEntrust* entrust = WTSEntrust::create("test", 1.0, 100.0);
        ASSERT_NE(entrust, nullptr);
        
        // 增加引用计数
        entrust->retain();
        EXPECT_GT(entrust->retainCount(), 1);
        
        // 释放引用
        entrust->release();
        entrust->release();
    }
    
    // 测试订单对象内存管理
    {
        WTSOrderInfo* order = WTSOrderInfo::create();
        ASSERT_NE(order, nullptr);
        
        order->retain();
        EXPECT_GT(order->retainCount(), 1);
        
        order->release();
        order->release();
    }
    
    // 测试成交对象内存管理
    {
        WTSTradeInfo* trade = WTSTradeInfo::create("test", "TEST");
        ASSERT_NE(trade, nullptr);
        
        trade->retain();
        EXPECT_GT(trade->retainCount(), 1);
        
        trade->release();
        trade->release();
    }
    
    // 测试持仓对象内存管理
    {
        WTSPositionItem* position = WTSPositionItem::create("test", "CNY", "TEST");
        ASSERT_NE(position, nullptr);
        
        position->retain();
        EXPECT_GT(position->retainCount(), 1);
        
        position->release();
        position->release();
    }
    
    // 测试账户对象内存管理
    {
        WTSAccountInfo* account = WTSAccountInfo::create();
        ASSERT_NE(account, nullptr);
        
        account->retain();
        EXPECT_GT(account->retainCount(), 1);
        
        account->release();
        account->release();
    }
}

/*! 
 * \brief 测试交易数据的边界条件
 * 
 * \details 验证交易数据在边界条件下的行为：
 *          - 空字符串处理
 *          - 极值数据处理
 *          - 无效参数处理
 *          - 异常状态处理
 */
TEST_F(WTSTradeDefTest, TestBoundaryConditions) {
    // 测试空字符串参数
    WTSEntrust* entrust = WTSEntrust::create("", 0.0, 0.0, "");
    ASSERT_NE(entrust, nullptr);
    EXPECT_STREQ(entrust->getCode(), "");
    EXPECT_STREQ(entrust->getExchg(), "");
    EXPECT_DOUBLE_EQ(entrust->getVolume(), 0.0);
    EXPECT_DOUBLE_EQ(entrust->getPrice(), 0.0);
    entrust->release();
    
    // 测试极值数据
    WTSOrderInfo* order = WTSOrderInfo::create();
    ASSERT_NE(order, nullptr);
    
    order->setVolume(DBL_MAX);
    order->setPrice(DBL_MAX);
    order->setOrderTime(UINT64_MAX);
    
    EXPECT_DOUBLE_EQ(order->getVolume(), DBL_MAX);
    EXPECT_DOUBLE_EQ(order->getPrice(), DBL_MAX);
    EXPECT_EQ(order->getOrderTime(), UINT64_MAX);
    
    order->release();
    
    // 测试负数处理
    WTSTradeInfo* trade = WTSTradeInfo::create("test", "TEST");
    ASSERT_NE(trade, nullptr);
    
    trade->setVolume(-1.0);
    trade->setPrice(-100.0);
    trade->setAmount(-100.0);
    
    EXPECT_DOUBLE_EQ(trade->getVolume(), -1.0);
    EXPECT_DOUBLE_EQ(trade->getPrice(), -100.0);
    EXPECT_DOUBLE_EQ(trade->getAmount(), -100.0);
    
    trade->release();
}

/*! 
 * \brief 测试交易数据的完整工作流程
 * 
 * \details 验证交易数据在完整交易流程中的使用：
 *          - 委托下单流程
 *          - 订单状态跟踪
 *          - 成交信息处理
 *          - 持仓更新计算
 */
TEST_F(WTSTradeDefTest, TestCompleteWorkflow) {
    // 1. 创建委托
    WTSEntrust* entrust = WTSEntrust::create("au2412", 10.0, 380.5, "SHFE");
    ASSERT_NE(entrust, nullptr);
    
    entrust->setDirection(WDT_LONG);
    entrust->setOffsetType(WOT_OPEN);
    entrust->setPriceType(WPT_LIMITPRICE);
    entrust->setEntrustID("E001");
    
    // 2. 创建订单（委托提交后生成）
    WTSOrderInfo* order = WTSOrderInfo::create();
    ASSERT_NE(order, nullptr);
    
    order->setCode("au2412");
    order->setExchange("SHFE");
    order->setDirection(entrust->getDirection());
    order->setOffsetType(entrust->getOffsetType());
    order->setPriceType(entrust->getPriceType());
    order->setVolume(entrust->getVolume());
    order->setPrice(entrust->getPrice());
    order->setOrderID("O001");
    order->setOrderState(WOS_NotTraded_Queuing);
    
    // 验证订单信息与委托一致
    EXPECT_EQ(order->getDirection(), entrust->getDirection());
    EXPECT_EQ(order->getOffsetType(), entrust->getOffsetType());
    EXPECT_DOUBLE_EQ(order->getVolume(), entrust->getVolume());
    EXPECT_DOUBLE_EQ(order->getPrice(), entrust->getPrice());
    
    // 3. 模拟部分成交
    WTSTradeInfo* trade1 = WTSTradeInfo::create("au2412", "SHFE");
    ASSERT_NE(trade1, nullptr);
    
    trade1->setDirection(WDT_LONG);
    trade1->setOffsetType(WOT_OPEN);
    trade1->setVolume(5.0);
    trade1->setPrice(380.6);
    trade1->setAmount(1903.0);
    trade1->setTradeID("T001");
    trade1->setRefOrder("O001");
    
    // 更新订单状态
    order->setVolTraded(5.0);
    order->setVolLeft(5.0);
    order->setOrderState(WOS_PartTraded_Queuing);
    
    // 4. 模拟剩余成交
    WTSTradeInfo* trade2 = WTSTradeInfo::create("au2412", "SHFE");
    ASSERT_NE(trade2, nullptr);
    
    trade2->setDirection(WDT_LONG);
    trade2->setOffsetType(WOT_OPEN);
    trade2->setVolume(5.0);
    trade2->setPrice(380.7);
    trade2->setAmount(1903.5);
    trade2->setTradeID("T002");
    trade2->setRefOrder("O001");
    
    // 更新订单状态为全部成交
    order->setVolTraded(10.0);
    order->setVolLeft(0.0);
    order->setOrderState(WOS_AllTraded);
    
    // 5. 更新持仓
    WTSPositionItem* position = WTSPositionItem::create("au2412", "CNY", "SHFE");
    ASSERT_NE(position, nullptr);
    
    position->setDirection(WDT_LONG);
    position->setNewPosition(10.0);
    position->setAvailNewPos(10.0);
    position->setAvgPrice((trade1->getPrice() * trade1->getVolume() + 
                          trade2->getPrice() * trade2->getVolume()) / 
                         (trade1->getVolume() + trade2->getVolume()));
    
    // 验证持仓计算
    EXPECT_DOUBLE_EQ(position->getTotalPosition(), 10.0);
    EXPECT_DOUBLE_EQ(position->getAvailPosition(), 10.0);
    EXPECT_NEAR(position->getAvgPrice(), 380.65, 0.01);
    
    std::cout << "交易工作流程测试通过" << std::endl;
    std::cout << "委托数量: " << entrust->getVolume() << std::endl;
    std::cout << "成交数量: " << order->getVolTraded() << std::endl;
    std::cout << "持仓数量: " << position->getTotalPosition() << std::endl;
    std::cout << "持仓均价: " << position->getAvgPrice() << std::endl;
    
    // 清理资源
    entrust->release();
    order->release();
    trade1->release();
    trade2->release();
    position->release();
}